package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书....");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书：");
        String name = scanner.nextLine();
        int size = bookList.getBooksize();
        int pos = 0;
        int i = 0;
        for (; i < size; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("找到了！");
                System.out.println(book);
                pos = i;
                break;
            }
        }
            if(size == i) {
                System.out.println("没有这本书！");
                return;
            }
                //删除
                //booklist[j] = booklist[j+1]
        for (int j = pos; j < size - 1; j++) {
            Book newbook = bookList.getBook(j + 1);
            bookList.setBook(j, newbook);
        }
        //有效个数-1
        bookList.setBooksize(size - 1);
        System.out.println("删除成功！");
    }
}
